﻿using System;
using System.Linq;
using Colligate;

namespace OrgDataSource
{
    public class UserVarDS:IOrgDS
    {
        #region\\delete
        public int DeleteTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, orgEntities context)
        {
            switch (DataType.ToLower())
            {
                case "uservar": return DeleteUserVar(DataGuid, context);
                default: throw new Exception("UserVarDS无法处理该" + DataType + "的数据");
            }
        }

        private static int DeleteUserVar(string DataGuid, orgEntities context)
        {
            try
            {
                var uservar = context.UserVar.FirstOrDefault(cc => cc.Guid == DataGuid);
                context.DeleteObject(uservar);
                var result = context.SaveChanges();
                return result == 1 ? 0 : 1;
            }
            catch
            {
                throw new Exception("删除数据标识为：" + DataGuid + "的UserVar数据时出错！");
            }

        }
        #endregion
        #region\\get
        public System.Xml.XmlNode GetTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, orgEntities context, BaseTaskDataSource baseInfo)
        {
            switch (DataType.ToLower())
            {
                case "uservar": return GetUserVar(DataGuid, context);
                default: throw new Exception("UserVarDS无法处理该" + DataType + "的数据");
            }
        }

        private static System.Xml.XmlNode GetUserVar(string DataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), DataGuid, "UserVar");
                var uservar = context.UserVar.FirstOrDefault(cc => cc.Guid == DataGuid) ?? new UserVar
                                                                                               {
                                                                                                   Guid = string.Empty,
                                                                                                   OUGuid = string.Empty,
                                                                                                   VarValue = string.Empty
                                                                                               };
                XmlNodeManage.SetSubValue(result, "Guid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), uservar.Guid);
                XmlNodeManage.SetSubValue(result, "OUGuid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), uservar.OUGuid);
                XmlNodeManage.SetSubValue(result, "VarValue", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), uservar.VarValue);
                //var varList=uservar.VarValue.Replace("/Var>","/Var>,").Trim(',').Split(',');
                return result;
            }
            catch
            {
                throw new Exception("获取数据标识为：" + DataGuid + "的UserVar数据时出错！");
            }
        }
        #endregion
        #region\\set
        public int SetTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, System.Xml.XmlNode XmlData, orgEntities context)
        {
            var datatype = XmlNodeManage.GetSubValue(XmlData, "@DataType");
            switch (datatype.ToLower())
            {
                case "uservar": return SetUserVar(XmlData, context);
                default: throw new Exception("UserVarDS无法处理该" + datatype + "的数据");
            }
        }

        private static int SetUserVar(System.Xml.XmlNode xmlData, orgEntities context)
        {
            try
            {
                var id = Utility.CheckXmlData(xmlData,"Guid");
                var uservar = context.UserVar.FirstOrDefault(cc => cc.Guid == id);
                if (null == uservar)
                {
                    uservar = new UserVar
                                  {
                        Guid = id,
                        OUGuid = Utility.CheckXmlData(xmlData,"OUGuid"),
                        VarValue = Utility.CheckXmlData(xmlData,"VarValue")
                    };
                    context.AddToUserVar(uservar);
                }
                else
                {
                    uservar.Guid = Utility.CheckXmlData(xmlData,"Guid");
                    uservar.OUGuid = Utility.CheckXmlData(xmlData,"OUGuid");
                    uservar.VarValue = Utility.CheckXmlData(xmlData,"VarValue");
                }
                var result = context.SaveChanges();
                return result == 1 ? 0 : 1;
            }
            catch
            {
                throw new Exception("写入UserVar数据时出错！");
            }
        }
        #endregion
        #region\\transform
        public System.Xml.XmlNode TransformTaskData(OUDataInfo UserInfo, System.Xml.XmlNode xmlParam, orgEntities context, BaseTaskDataSource baseInfo)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
